<template>
  <div class="access-control">
    <el-card class="control-card" shadow="hover">
      <div slot="header" class="card-header">
        <span><i class="el-icon-lock"></i> 访问控制</span>
        <el-button type="primary" @click="showAddPolicyDialog" icon="el-icon-plus">新增策略</el-button>
      </div>

      <!-- 搜索栏 -->
      <div class="operation-bar">
        <div class="left-operations">
          <el-button-group>
            <el-button size="small" icon="el-icon-refresh" @click="refreshList">刷新</el-button>
          </el-button-group>
        </div>
        <div class="search-box">
          <el-input
            v-model="searchQuery"
            placeholder="搜索IP或策略内容"
            prefix-icon="el-icon-search"
            clearable
            @clear="handleSearch"
            @keyup.enter.native="handleSearch">
          </el-input>
        </div>
      </div>

      <!-- 策略列表 -->
      <el-table
        :data="filteredTableData"
        style="width: 100%"
        border
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        stripe>
        <el-table-column prop="ip" label="授权对象" min-width="180">
          <template slot-scope="scope">
            <el-tooltip :content="scope.row.ip" placement="top">
              <span class="ip-text">{{ scope.row.ip }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" min-width="300">
          <template slot-scope="scope">
            <span>{{ scope.row.description || '--' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="180">
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              class="delete-button"
              @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <div class="pagination-container">
        <span class="total-count">共 {{ total }} 条数据</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 新增策略对话框 -->
    <el-dialog
      title="新增访问策略"
      :visible.sync="policyDialogVisible"
      width="500px"
      :close-on-click-modal="false"
      @closed="resetPolicyForm">
      <el-form :model="policyForm" :rules="policyRules" ref="policyForm" label-width="100px">
        <el-form-item label="IP地址" prop="ip" required>
          <el-input
            v-model="policyForm.ip"
            placeholder="请输入IP地址，例如：10.20.3.30">
          </el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            type="textarea"
            :rows="3"
            v-model="policyForm.description"
            placeholder="请输入策略描述信息">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="policyDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handlePolicySubmit" :loading="submitting">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 删除确认对话框 -->
    <el-dialog
      title="确认删除"
      :visible.sync="deleteDialogVisible"
      width="400px">
      <div class="delete-confirm-content">
        <i class="el-icon-warning"></i>
        <p>确定要删除该访问策略吗？此操作不可恢复。</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="confirmDelete" :loading="deleting">确定删除</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'AccessControl',
  data() {
    return {
      // 搜索相关
      searchQuery: '',
      loading: false,

      // 表格数据
      tableData: [
        {
          ip: '10.20.3.30',
          description: '',
          createTime: '2024-04-26 17:48:37'
        }
      ],

      // 分页相关
      currentPage: 1,
      pageSize: 10,
      total: 1,

      // 新增策略对话框
      policyDialogVisible: false,
      submitting: false,
      policyForm: {
        ip: '',
        description: ''
      },
      policyRules: {
        ip: [
          { required: true, message: '请输入IP地址', trigger: 'blur' },
          { pattern: /^(\d{1,3}\.){3}\d{1,3}$/, message: '请输入正确的IP地址格式', trigger: 'blur' }
        ],
        description: [
          { max: 200, message: '描述长度不能超过200个字符', trigger: 'blur' }
        ]
      },

      // 删除相关
      deleteDialogVisible: false,
      deleting: false,
      currentDeleteItem: null
    }
  },
  computed: {
    filteredTableData() {
      if (!this.searchQuery) {
        return this.tableData
      }
      const query = this.searchQuery.toLowerCase()
      return this.tableData.filter(data => {
        return data.ip.toLowerCase().includes(query) ||
          (data.description && data.description.toLowerCase().includes(query))
      })
    }
  },
  methods: {
    // 搜索相关方法
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },

    // 分页相关方法
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 新增策略相关方法
    showAddPolicyDialog() {
      this.policyDialogVisible = true
    },
    resetPolicyForm() {
      if (this.$refs.policyForm) {
        this.$refs.policyForm.resetFields()
      }
      this.policyForm = {
        ip: '',
        description: ''
      }
    },
    handlePolicySubmit() {
      this.$refs.policyForm.validate((valid) => {
        if (valid) {
          this.submitting = true
          // 模拟API调用
          setTimeout(() => {
            this.submitting = false
            this.policyDialogVisible = false
            this.$message({
              message: '策略添加成功',
              type: 'success'
            })
            this.getList()
          }, 1000)
        }
      })
    },

    // 删除相关方法
    handleDelete(row) {
      this.currentDeleteItem = row
      this.deleteDialogVisible = true
    },
    confirmDelete() {
      this.deleting = true
      // 模拟API调用
      setTimeout(() => {
        this.deleting = false
        this.deleteDialogVisible = false
        this.$message({
          message: '策略删除成功',
          type: 'success'
        })
        this.getList()
      }, 1000)
    },

    // 获取列表数据
    getList() {
      this.loading = true
      // 模拟API调用
      setTimeout(() => {
        this.loading = false
      }, 800)
    },

    // 刷新列表
    refreshList() {
      this.getList()
    },

    // 格式化日期时间
    formatDateTime(datetime) {
      if (!datetime) return '--'
      return datetime
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
.access-control {
  padding: 0px;
}

.control-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header i {
  margin-right: 8px;
  font-size: 18px;
}

.operation-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.left-operations {
  display: flex;
  gap: 10px;
}

.search-box {
  width: 300px;
}

.ip-text {
  font-family: monospace;
  color: #409EFF;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.total-count {
  color: #606266;
  font-size: 14px;
}

/* 表格样式优化 */
.el-table {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;
}

.el-table th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
}

.el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 删除按钮样式 */
.delete-button {
  color: #F56C6C;
}

.delete-button:hover {
  color: #f78989;
}

/* 删除确认对话框样式 */
.delete-confirm-content {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 0;
}

.delete-confirm-content i {
  font-size: 24px;
  color: #E6A23C;
}

.delete-confirm-content p {
  margin: 0;
  color: #606266;
}

/* 对话框样式优化 */
.el-dialog__header {
  border-bottom: 1px solid #ebeef5;
  padding: 15px 20px;
}

.el-dialog__footer {
  border-top: 1px solid #ebeef5;
  padding: 15px 20px;
}

.el-dialog__body {
  padding: 30px 20px;
}

/* 加载动画样式 */
.el-loading-spinner .el-icon-loading {
  font-size: 30px;
  color: #409EFF;
}

.el-loading-spinner .el-loading-text {
  color: #409EFF;
  margin: 3px 0;
  font-size: 14px;
}
</style>
